// uni.request请求封装
const baseUrl = 'https://www.tiktok-c.shop/';//请求的根路径www.qd20231105.test
// const baseUrl = 'http://www.qd20231105.test/';
// const baseUrl = 'http://qd3.sjdj888.online/';
let token = '';//token令牌
const request = (options = {},is_loading) => {
	uni.getStorageSync('token') &&(token = uni.getStorageSync('token'));//从本地缓存中获取token
	return new Promise((resolve, reject)=>{
		// 配置默认请求头
		options.header = {
			 'Authorization': `${token}`,
			 // 'language': uni.getStorageSync('language'),
		};
		if(is_loading == true){
			uni.showLoading({
				title: 'loading....'
			});
		}
		
		// options.data.lang = uni.getStorageSync('language')
		let lang = uni.getStorageSync('language');
		let langs = 'en-ww';
		if(lang == 'tr'){
			langs = 'tr-tr'
		}else if(lang == 'en'){
			langs = 'en-ww'
		}else if(lang == 'jp'){
			langs = 'jr-rbg'
		}else if(lang == 'zh-CN'){
			langs = 'zh-cn'
		}else if(lang == 'ar'){
			langs = 'ar-oa'
		}else if(lang == 'es'){
			langs = 'es-mx'
		}else if(lang == 'sq'){
			langs = 'sq'
		}
		uni.request({
			url: baseUrl + options.url+'?lang='+langs || '',
			method: options.method || 'GET', 
			data: options.data || {},
			header: options.header || {},
			success: (res) => {
				if(is_loading == true){
					uni.hideLoading();
				}
				// console.log(77777777);
				if (res.data.code == 0) {//成功
		            resolve(res.data, res);
		        }else if (res.data.code == 401) {//未登录
				// console.log(66666);
					uni.showToast({
						title:'Unauthorized',
						icon: 'none',
						duration: 1500
					});
		   //          uni.reLaunch({
		   //              url: "/pages/Login/login"
		   //          });
					// return;
					getApp().globalData.t3 = setTimeout(()=>{
						uni.reLaunch({
						    url: "/pages/Login/login"
						});
					},1500);
					return;
		        }else if (res.data.code== 1) {//错误
				// console.log(res.data.info)
				// uni.showModal({
				// 	content:res.data.info || 'error'
				// })
		            uni.showToast({
		                title: res.data.info || 'error',
		                icon: 'none',
		                duration: 1500
		            });
					// reject(res.data.message || '系统错误');
		        }else if (res.data.code== 3) {//有订单未完成，跳转做单页面
				// console.log(res.data.message)
		            uni.showToast({
		                title: res.data.info || 'error',
		                icon: 'none',
		                duration: 1500
		            });
					lgetApp().globalData.t1 =  setTimeout( ()=> {
						uni.switchTab({
							url:"/pages/Balance/index"
						})
					},1500);
					return;
					// reject(res.data.message || '系统错误');
		        }else if (res.data.code== 4) {//未添加提币地址，跳转个人信息页面
				// console.log(res.data.message)
		            uni.showToast({
		                title: res.data.info || 'error',
		                icon: 'none',
		                duration: 1500
		            });
					getApp().globalData.t2 = setTimeout(()=>{
						uni.switchTab({
							url:"/pages/My/personInfo/personInfo"
						})
					},1500);
					
					// reject(res.data.message || '系统错误');
		        }else{
					uni.showToast({
					    title: res.data.info || 'error',
					    icon: 'none',
					    duration: 1500
					});
					// reject(res.data.message || '系统错误');
				}
			},
		    fail: (msg) => {
				// console.log(888888)
				if(is_loading == true){
					uni.hideLoading();
				}
				uni.showToast({
				    title: 'error',
				    icon: 'none',
				    duration: 1500
				});
		        // reject('请求失败');
		    },
			complete() {
				uni.hideLoading();
			}
		  
		})
  })
}
 
// GET请求
const get = (url, data={}, options={}, is_loading = true) => {
  options.url = url
  options.method = 'GET'
  options.data = data
  return request(options, is_loading)
}
 
// POST请求
const post = (url, data={}, options={}, is_loading = true) => {
  options.url = url
  options.method = 'POST'
  options.data = data
  return request(options, is_loading)
}
 
// 默认向外暴露的数据
export default  {
    request,
	baseUrl,
    get,
    post
}